'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 活动副本
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var soldierAttr = require('./soldierAttr');
var buildAttr = require('./buildAttr');
var buildType = require('../../consts/buildType');
var buildConf = require('../../config/buildConf');
var copyService = require('../../services/copyService');
var actCarbonConf = require('../../config/actCarbonConf.js');
var constantUtil = require('../../util/constantUtil');
var buildTypeUtil = require('../../util/buildTypeUtil');
var techService = require('../../services/techService');
var buildService = require('../../services/buildService.js');
var dataApi = require('../../libs/dataApi.js');

/**
 * 获取副本数据
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
exports.getData = function (carbon_id) {
    var small_conf = actCarbonConf.getRandSmallConf(carbon_id);
    var small_id = small_conf.small_id;
    var carbon_conf = actCarbonConf.getCarbonConfById(carbon_id);
    var copy_build_conf = actCarbonConf.getCarbonBuildConf(small_id);
    return {
        name: carbon_conf.name,
        scene_name: small_conf.scene_name,
        road_list: _getRoadList(copy_build_conf),
        build_list: _getBuildList(copy_build_conf),
        soldier_list: _getCopySoldierList(carbon_conf, copy_build_conf),
    };
};

/**
 * 获取道路
 * @param  {[type]} copy_build_conf [description]
 * @return {[type]}         [description]
 */
var _getRoadList = function (copy_build_conf) {
    var road_list = [];
    _.forEach(copy_build_conf, function (build_conf) {
        if (build_conf.building_id == buildType.ROAD) {
            road_list.push(build_conf.coordinate);
        }
    });
    return road_list;
};

/**
 * 获取建筑列表
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
var _getBuildList = function (carbon_build_conf) {
    var build_list = [];
    _.forEach(carbon_build_conf, function (build_conf) {
        if (build_conf.building_id != buildType.ROAD) {
            var build_type = buildTypeUtil.getBuildType(build_conf.building_id);
            var building_type_conf = buildConf.getBuildTypeConf(build_type);

            build_list.push(_getBuild(build_conf));
        }
    });
    return build_list;
};

/**
 * 获取副本士兵列表
 * @param  {[type]} copy_id       [description]
 * @return {[type]}               [description]
 */
var _getCopySoldierList = function (carbon_conf, copy_build_conf) {
    var copy_soldier_list = [];
    var soldier_ratio = carbon_conf.soldier_ratio;
    _.forEach(copy_build_conf, function (build_conf) {
        var soldier_info = build_conf.soldier_info;
        if (soldier_info.length <= 0) {
            return true;
        }
        copy_soldier_list = _getSoldierList(build_conf.soldier_info, soldier_ratio);
        return false;
    });
    return copy_soldier_list;
};

/**
 * 获取士兵列表
 * @param  {[type]} copy_build_conf       [description]
 * @return {[type]}               [description]
 */
var _getSoldierList = function (soldier_info_str, soldier_ratio) {
    var soldier_list = [];
    var soldier_type_lv_list = {};
    var wall_soldier_list = _parseWallSoldierList(soldier_info_str);
    _.forEach(wall_soldier_list, function (batch_soldier_list) {
        var batch_info = [];
        _.forEach(batch_soldier_list, function (soldier_item) {
            var soldier_type = soldier_item.soldier_type;
            var soldier_lv = soldier_item.soldier_lv;
            var soldier_star = soldier_item.soldier_star;
            soldier_type_lv_list[soldier_type] = {
                soldier_lv: soldier_lv,
                soldier_star: soldier_star
            };
        });
    });

    var error = false;
    var ratio_keys = ['min_atk', 'max_atk', 'fire_damage', 'ice_damage', 'light_damage', 'city_damage', 'hp'];
    _.forEach(soldier_type_lv_list, function (soldier_lv_conf, soldier_type) {
        var soldier_info = soldierAttr.getAttr(
            soldier_type,
            soldier_lv_conf.soldier_lv,
            soldier_lv_conf.soldier_star
        );
        if (!soldier_info) {
            error = true;
        }

        if (!!soldier_ratio) {
            _.forEach(ratio_keys, function (fld) {
                soldier_info.attr[fld] = Math.ceil(soldier_info.attr[fld] * soldier_ratio / 100);
            });
        }

        soldier_list.push(soldier_info);
    });

    if (!!error) {
        return [];
    }

    return soldier_list;
};

/**
 * 获取建筑的item
 * @param  {[type]} build_conf [description]
 * @return {[type]}            [description]
 */
var _getBuild = function (build_conf, build_ratio) {
    var build_id = build_conf.building_id;
    var build_lv = build_conf.building_lvl;
    var build_cell = build_conf.coordinate;
    var build_star = build_conf.building_star;
    var build_type = buildTypeUtil.getBuildType(build_id);

    var build_item = {
        build_id: build_id,
        cell: build_cell,
        star: build_star,
        attr: buildAttr.getAttr(build_type, build_lv, build_ratio)
    };

    // 附加星级加成
    buildService.getBuildStarEff(build_item);

    // var tech_type_list = ['tech_type1', 'tech_type2', 'tech_type3', 'tech_type4'];
    // var tech_value_list = ['tech_value1', 'tech_value2', 'tech_value3', 'tech_value4'];
    // for (var i = 0; i < tech_type_list.length; i++) {
    //     var tech_type = build_conf[tech_type_list[i]];
    //     var tech_value = build_conf[tech_value_list[i]];
    //     if (!!tech_type && !!tech_value) {
    //         techService.addBuildTechEff(build_item, tech_type, tech_value);
    //     }
    // }

    if (build_type == buildType.WALL) {
        build_item.soldier_list = _getBuildSoldierList(build_conf.soldier_info);
    }

    return build_item;
};

/**
 * 获取兵营的兵种数据
 * @return {Array} [description]
 */
var _getBuildSoldierList = function (wall_soldier_info) {
    var smpl_soldier_list = [];

    var wall_soldier_list = _parseWallSoldierList(wall_soldier_info);
    _.forEach(wall_soldier_list, function (batch_soldier_list) {
        var batch_info = [];
        _.forEach(batch_soldier_list, function (soldier_item) {
            batch_info.push(soldier_item.soldier_type);
        });

        smpl_soldier_list.push({
            single_soldier_list: batch_info
        });
    });
    return smpl_soldier_list;
};

/**
 * 获取城墙的兵种数据
 * @return {Array} [description]
 */
var _parseWallSoldierList = function (wall_soldier_info) {
    var wall_soldier_list = [];
    if (!wall_soldier_info || wall_soldier_info.length <= 0) {
        return wall_soldier_list;
    }
    var batch_info_arr = wall_soldier_info.split(';');
    _.forEach(batch_info_arr, function (batch_info) {
        wall_soldier_list.push(_parseSoldierList(batch_info));
    });
    return wall_soldier_list;
};

/**
 * 获取批次的兵种数据
 * @return {Array} [description]
 */
var _parseSoldierList = function (batch_soldier_info) {
    var soldier_list = [];
    if (batch_soldier_info.length <= 0) {
        return soldier_list;
    }

    var soldier_info_list = batch_soldier_info.split(',');
    _.forEach(soldier_info_list, function (soldier_info_item) {
        var soldier_info = soldier_info_item.split(':');
        var soldier_type = parseInt(soldier_info[0]);
        var soldier_lv = parseInt(soldier_info[1]);
        var soldier_star = parseInt(soldier_info[2]);

        soldier_list.push({
            soldier_type: soldier_type,
            soldier_lv: soldier_lv,
            soldier_star: soldier_star
        });
    });
    return soldier_list;
};

exports.getBuildSoldierList = function (wall_soldier_info) {
    return _getBuildSoldierList(wall_soldier_info);
};

exports.getSoldierList = function (wall_soldier_info) {
    return _getSoldierList(wall_soldier_info);
};
